using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;

namespace LeanUi.ImageBundle
{
    static class JpegHelper
    {
        public static void SaveJpg(this Bitmap jpgImage, String pathToSave, Int64 quality)
        {
            ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

            var qualityParam = new EncoderParameters(1);
            qualityParam.Param[0] = new EncoderParameter(Encoder.Quality, quality);

            jpgImage.Save(pathToSave, jgpEncoder, qualityParam);
        }

        private static ImageCodecInfo GetEncoder(this ImageFormat format)
        {
            return ImageCodecInfo.GetImageDecoders().Where(c => c.FormatID == format.Guid).First();
        }
    }
}